/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GameClass;

import Background.Background;
import Background.Ground;
import Background.Sound;
import Background.Wall;
import CharacterType.Huntress;
import Core.Vector2;
import Core.Way;
import EffectType.Explode;
import Environment.Gravity;
import Environment.Windy;
import Game.GameDesign;
import GiftType.ActiveGift;
import GiftType.Reward;
import MonsterType.Land;
import Thing.Character;
import Thing.Effect;
import Thing.Gift;
import Thing.Monster;
import java.util.Random;
import java.util.Vector;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.TiledLayer;

/**
 *
 * @author THINH
 */
//Component is 1 type of game play. ex : arcade, multiple, etc...
//Each component include many scenes.
//Each component is in 1 class.
public class Component extends LayerManager{
    //field
    Gravity  gravity;
    Windy windy;
    Character player;
    Background ground; 
    Sound sound;
    Wall wall;
    Background backGround;
    GameDesign design;
    TiledLayer currentMap = null;
    int[] stand = {0,0,0,0,0,1,1,1,1,1,2,2,2,2,2,3,3,3,3,3};
    int[] goRight = {4,4,4,4,4,5,5,5,5,5,6,6,6,6,6,7,7,7,7,7};
    int[] MonstergoRight = {6,6,6,6,7,7,7,7,8,8,8,8};
    int[] MonsterShoot = {7,7,7,8,8,8};
    //int[] jump = {8,9,10,11};
    int[] jump = {8,9,10,11};
    int[] sit = {11};
    int[] shootHigh= {16,17,18,19,20,21,22};
    int[] shootNormal= {24,24,25,25,26,26,27,27,28,28,29,29};
    int[] explodeSeq = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
    Vector monsters = new Vector(0);
    Vector effects = new Vector(0);
    public Vector item = new Vector(0);
   
    int mapWidth = 0;
    int mapHeight = 0;
    public boolean gameover = false;
    public boolean gameWin = false;
    int activeItem = 2;
    int rewardItem = 1;
    private static final int Win = 3;
    public int MonsterKill = 0;
    private String[] activeGift = {"/resource/item/Science_icon.jpg"};
    private String[] reward = {"/resource/item/Culture_icon.jpg","/resource/item/Food_icon.jpg",
                                "/resource/item/Gold_icon.jpg","/resource/item/Production_icon.jpg"};
    private int[] seq = {0,0,0,0};
    Random ran = new Random();
    private int countHit = 0;
    private static final int countMax = 1;
    //constructor
    public Component(int width, int height){
        LoadContent();
        //code here
    }
    //load content
    private void LoadContent(){
        //code here
        windy = new Windy();
        gravity = new Gravity();
        //sound = new Sound("/smb1-Theme.mid");
        player = new Huntress("/resource/huntress.png",47,47,stand,goRight,jump,shootHigh,shootNormal, sit);
        
        insert(player.getSprite(),0);
        backGround = new Background();
        wall = new Wall();
        ground = new Ground();        
        design = new GameDesign();
        
        createMap_Snow();
        
    }
    //main func
    public void active(String code)
    {
        if(code.equals("Q"))
        {
            player.activeGift(0);
        }
        else if(code.equals("E"))
        {
            player.activeGift(1);
        }
        else if(code.equals("R"))
        {
            player.activeGift(2);
        }
    }
    
    public boolean update(int keyStates){
        boolean ret = false;
        
        //code here
        //--------------------key Item--------------------------------------
       /*
        if((keyStates&GameCanvas.UP_PRESSED)+(keyStates&GameCanvas.DOWN_PRESSED)+(keyStates&GameCanvas.LEFT_PRESSED)+(keyStates&GameCanvas.RIGHT_PRESSED) == 0){
            if ((keyStates & GameCanvas.KEY_NUM1) != 0) {
                player.activeGift(0);
            } else if ((keyStates & GameCanvas.KEY_NUM2) != 0) {
                player.activeGift(1);
            } else if ((keyStates & GameCanvas.KEY_NUM3) != 0) {
                player.activeGift(2);
            }
        }
         * 
         */
        //-------------------------input-------------------------------------
        if((keyStates&GameCanvas.FIRE_PRESSED)!=0){
            if(player.flying==null && !player.status.isGo()){
                
                if(player.status.isRight()){
                    player.shoot(new Vector2(13,0));
                }
                else{
                    player.shoot(new Vector2(-13,0));
                }
            }
        }
        if((keyStates&GameCanvas.UP_PRESSED)!=0){
            player.move(Way.UP);
        }
        
        if((keyStates&GameCanvas.RIGHT_PRESSED)!=0){
            player.move(Way.RIGHT);
        }
        else if((keyStates&GameCanvas.LEFT_PRESSED)!=0){
            player.move(Way.LEFT);
        }
        else if((keyStates&GameCanvas.DOWN_PRESSED)!=0){
            player.sit();
        }
        else {
            if(!player.status.isJump())
                player.stand();
        }
        
        
        
        //-------------------------update----------------------------------
        //player
        player.update();
        gravity.Enable(player);
        //flying of player
        if(player.flying!=null ){
            insert(player.flying.getSprite(),1);
            if(!player.flying.isLive){
                remove(player.flying.getSprite());
                player.flying = null;
            }
        }
        //------------------------------------------------------------------
        //monster
        for(int i = 0 ; i < monsters.size(); i++){
            Monster mon = (Monster)monsters.elementAt(i);
            insert(mon.getSprite(),1);
            mon.update();
            
            //gravity.Enable(mon);
            //flying of mon
            if (mon.fly != null) {
                insert(mon.fly.getSprite(), 1);
                if (!mon.fly.isLive) {
                    remove(mon.fly.getSprite());
                    mon.fly = null;
                }
            }
            
         //------------------------Colision of mon-----------------------------------
            //monster hit ground
            while(mon.getSprite().collidesWith(ground.getTiledLayer(), true)){
                ground.CollisionWith(mon);                
            }
            //monster hit wall
            boolean hitWall = false;
            while(mon.getSprite().collidesWith(wall.getTiledLayer(), true)){
                wall.CollisionWith(mon);                
                if(!hitWall){                    
                    mon.range =-1*mon.range;
                    hitWall = true;
                }
                
            }
            //monster hit flying
            if(player.flying!=null){
                if(mon.getSprite().collidesWith(player.flying.getSprite(), false)){
                   /* countHit++;
                    if(countHit >= countMax)
                    {
                        countHit = 0;
                        
                        ActiveGift gift = new ActiveGift("/resource/item/Science_icon.jpg", 40, 40, seq, player);
                        gift.setPos(mon.getSprite().getX(),mapHeight-100);
                        gravity.Enable(gift);
                        while (gift.getSprite().collidesWith(ground.getTiledLayer(), true)) {
                            ground.CollisionWith(gift);
                        }
                    }
                     * 
                     */
                    int damage = ((player.flying.getDamage()+player.damagebase+ player.damagebonus)*(100 - mon.getArmor()))/100;
                    mon.health = mon.health - damage;
                    //mon.health = 0;
                    remove(player.flying.getSprite());
                    player.flying = null;
                    
                    if(mon.health <= 0){
                        remove(mon.getSprite());
                        if (mon.fly != null) {
                            remove(mon.fly.getSprite());
                            mon.fly = null;
                        }
                        monsters.removeElementAt(i);
                        //Them 2 mon moi
                        for(int j = 0; j < 2; j++)
                        {
                            Monster m = new Land("/resource/Monster/flamedragon.png", 96, 96, MonstergoRight, MonsterShoot, player);
                            monsters.addElement(m);
                            m.setPos(mapWidth / 3+ j*300, mapHeight - 100);
                        }
                        //Them mot item
                        //ActiveGift gift = new ActiveGift("/resource/item/Science_icon.jpg", 40, 40, seq, player);
                        Gift gift = RandomItem();
                        if(gift == null)
                            System.out.println("null roi");
                        else
                            player.addGift(gift);
                        MonsterKill++;
                        if(MonsterKill >= Win)
                        {
                            gameWin = true;
                            for(int j = 0;j < monsters.size();j++)
                            {
                                Monster m = (Monster)monsters.elementAt(i);
                                remove(m.getSprite());
                            }
                        }
                    }  
                    Effect explode = new Explode(mon.getX(), mon.getY() , explodeSeq);
                    effects.addElement(explode);
                }
            }
            //monster hit player
            if(mon.getSprite().collidesWith(player.getSprite(), true)){
                int damage = (mon.getdamage())*((100-player.armor))/100;
                    player.currenthealth += -1*damage;
                    
                    if(player.currenthealth <= 0)
                    {
                        gameover = true;
                        //Hien man hinh ket thuc game                        
                    }
            }
                
            //flying of monster hit player
            if(mon.fly != null)
            {
                if(player.getSprite().collidesWith(mon.fly.getSprite(), false))
                {
                    int damage = (mon.fly.getDamage() + mon.getdamage())*((100-player.armor))/100;
                    player.currenthealth += -1*damage;
                    remove(mon.fly.getSprite());
                    mon.fly = null;
                    if(player.currenthealth <= 0)
                    {
                        gameover = true;
                        //Hien man hinh ket thuc game
                    }
                }
            }
        }
        
            
        //effect
        for(int i = 0 ; i < effects.size(); i++){
            Effect eff = (Effect)effects.elementAt(i);
            if(!eff.isLive){
                effects.removeElementAt(i);
                remove(eff.getSprite());
                i--;
            }
            else{
                insert(eff.getSprite(),1);
                eff.update();
            }
        }
         
        //-------------------------collision of huntress----------------------------------------
        //hit wall
        while(player.getSprite().collidesWith(wall.getTiledLayer(), true)){
            wall.CollisionWith(player);            
        }
        
        //hit ground
        while(player.getSprite().collidesWith(ground.getTiledLayer(), true)){
            if((player.status.isJump() && player.getVelocity().y>=0) || player.status.isShoot())
            {
               if(player.getVelocity().y>2)
               player.stand();
            }
            
            ground.CollisionWith(player);
        } 
       /*
        //hit bullet of mon or mon
        for(int i = 0; i < monsters.size(); i++)
        {
            Monster m = (Monster)monsters.elementAt(i);
            
        }
         * 
         */
        
        //hit item
        for(int i = 0; i < item.size();i++)
        {
            Gift g = (Gift)item.elementAt(i);
            if(player.getSprite().collidesWith(g.getSprite(), false))
            {
                player.addGift(g);
            }
        }
        return ret;
    }
        
        /*
        if(player.flying!=null){
            if(player.flying.getSprite().collidesWith(ground.getTiledLayer(), true)){
                remove(player.flying.getSprite());
                player.flyin7g = null;
            }
        }
         * 
         */
    
    private void createMap_Snow(){
        player.setPos(0,0);
        try{            
             
            //Tao gift
           // for(int i = 0; i < activeItem; i++)
            //{
              //  ActiveGift h = new ActiveGift();
               // h.setPos(i, i);
               // item.addElement(h);
            //}
            
            //Tao reward
           // for(int j = 0;j <rewardItem; j++)
            //{
              //  Reward r = new Reward();
               // r.setPos(j,j);
               // item.addElement(r);
            //}
            backGround.setTiledLayer(design.getMap1_Background());
            mapWidth = backGround.getWidth();
            mapHeight = backGround.getHeight();
            ground.setTiledLayer(design.getMap1_Ground());
            wall.setTiledLayer(design.getMap1_Wall());
            wall.setPos(-40,0);
            
            //Tao monster
            
            for(int i = 0 ; i < 1; i++){
                //Monster mon = new Monster("/resource/Monster/bonedragon.png",96, 96, MonstergoRight,MonsterShoot,player);
                Monster mon = new Land("/resource/Monster/flamedragon.png",96, 96, MonstergoRight,MonsterShoot,player);                
                monsters.addElement(mon);
                mon.setPos(mapWidth/2,mapHeight-100);
            }
             
            
            append(ground.getTiledLayer());            
            append(wall.getTiledLayer());
            append(backGround.getTiledLayer());
            
             
        }
        //catch(IOException ioe){
        //    ioe.printStackTrace();
        //}
        catch(Exception ex){
            System.out.print(ex.getMessage());
            ex.printStackTrace();
        }
        
    }
    
    private Gift RandomItem()
    {
        int type = ran.nextInt(1);
        if(type == 0)
        {
            ActiveGift gift = new ActiveGift(activeGift[0],40,40,seq,player);
            return gift;
        }
        else
        {
            int idx = Gift.Random(0, 3);
            Reward r = new Reward(reward[idx],40,40,seq,player);
            return r;
        }
    }
}
